# 登录功能
# name = input("请输入用户名")
# pwd = input("请输入密码")
# if name == '' and pwd == '':
#     print("登录成功")
# else:
#     print("登录失败")

# 大小写转换并存储在test.txt文件
# open('text.txt', 'w+').write(input("请输入小写字符").upper())

# 九九乘法表
# for i in range(1,10):
#     for j in range(1,i+1):
#         print(f"{j}*{i}={i*j}",end='\t')
#     print()

# 幸运数
# count = 0
# s = input("请输入一个六位数:\n")
# for str in s:
#     count += int(str)
# if count == (36 - 45):
#     print("幸运数")
# else:
#     print("不是幸运数")


import random


class Board(object):

    board = ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
    step = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    over = False
    win = [{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 4, 7},
           {2, 5, 8}, {3, 6, 9}, {1, 5, 9}, {3, 5, 7}]

    def show(self):
        for i in range(1, 10):
            print(self.board[i], end='')
            if (not(i % 3)):
                print()

    def begin(self, p1, p2):
        while (self.step or not(self.over)):

            # index = self.step[random.randint(0, len(self.step)-1)]
            index = random.sample(self.step, 1)[0]

            p1.play(self, index)
            self.calculate(p1)

            if self.over:
                break

            if not self.step:
                break

            # index = self.step[random.randint(0, len(self.step)-1)]
            index = random.sample(self.step, 1)[0]

            p2.play(self, index)
            self.calculate(p2)

            if self.over:
                break

        if not self.over:
            print('平局')

        self.show()

    def calculate(self, p) -> bool:
        print(p.icon, '->', p.step)
        for w in self.win:
            if p.step >= w:
                print(f'{p.icon} is win!')
                self.over = True
                break

    def reset(self):
        self.board = ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
        self.step = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        self.over = False


class Player(object):

    def __init__(self, icon: str) -> None:
        self.step = set()
        self.icon = icon

    def play(self, board: Board, index: int):
        board.board[index] = self.icon
        self.step.add(index)
        board.step.remove(index)


for i in range(10):
    board = Board()
    board.reset()
    p1 = Player('1')
    p2 = Player('0')
    board.begin(p1, p2)
    print()

